package com.sgs.preorder.web.controller;

import com.sgs.preorder.domain.dto.OrderDetailDTO;
import com.sgs.preorder.domain.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
@RefreshScope
@RequestMapping("/preorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @RequestMapping(value = "/detail/{orderNo}", method = RequestMethod.GET)
    public OrderDetailDTO getOrderDetail(@PathVariable("orderNo") String orderNo) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userId = request.getHeader("userId");
        OrderDetailDTO orderDetail = orderService.getOrderDetail(orderNo);
        if (StringUtils.isNoneBlank(userId)) {
            orderDetail.setUserId(Integer.valueOf(userId));
        }
        return orderDetail;
    }

}
